﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace BUS
{

    public class XuLyNhanVien
    {
        // ham tim danh sach nhan vien : ma loai la 'nv'
        public static DataTable TimToanBoNhanVien(String MaLoai)
        {
            DAO.CDataProvider reader = new DAO.CDataProvider();

            String sql = "SELECT * FROM NHANVIEN WHERE MaLoaiNV = '"+ MaLoai.ToString() +"' ";
            DataTable dt = new DataTable();
        
            try
            {
                dt = reader.DocDuLieu(sql);
            }
            catch (System.Exception )
            {
            	
            }
            return dt;
        }
         // Ham tim 1 nhan vien theo Ma Nhan Vien
        public static DTO.CNHANVIEN TimNhanVienTheoMaNV(int MaNV)
        {
            DTO.CNHANVIEN nv = new DTO.CNHANVIEN();
            // lop DataProvider
            DAO.CDataProvider reader = new DAO.CDataProvider();
            // chuoi SELECT
            String sql = " SELECT * FROM NHANVIEN " +
            " WHERE MaNhanVien = " + MaNV.ToString() + " ";

            try
            {
                // Bang data se duoc nhap
                DataTable dt = reader.DocDuLieu(sql);

                // Neu ton tai nhan vien nay
                if (dt.Rows.Count == 1)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        nv.HoTen = dr["HoTen"].ToString();
                        nv.MaNhanVien = int.Parse(dr["MaNhanVien"].ToString());
                        nv.Tuoi = int.Parse(dr["Tuoi"].ToString());
                        nv.DiaChi = dr["DiaChi"].ToString();
                        nv.MaLoaiNhanVien = dr["MaLoaiNV"].ToString();
                        nv.NgaySinh = DateTime.Parse(dr["NgaySinh"].ToString());
                    }
                }
                // nhan vien nay ko ton tai
                else
                {
                    nv.HoTen = "khong tim thay";
                    nv.MaNhanVien = 0;
                    nv.Tuoi = 0;
                    nv.DiaChi = "khong tim thay";
                    nv.MaLoaiNhanVien = "";
                    nv.NgaySinh = DateTime.Now;
                }

            }
            catch (System.Exception )
            {
            	
            }
            
            return nv;
        }

        // Ham them 1 nhan vien vao DB
        public static Boolean ThemNhanVien(DTO.CNHANVIEN nv)
        {
            DAO.CDataProvider writer = new DAO.CDataProvider();
            
            // Cau lenh SQL
            String insertSQL = " INSERT INTO NHANVIEN(HoTen,Tuoi,NgaySinh,DiaChi,MaLoaiNV) "
                + " VALUES('" + nv.HoTen.ToString() +"',"
                + nv.Tuoi +","
                + "'" + nv.NgaySinh.ToString() + "'," 
                + "'"+ nv.DiaChi.ToString() +"'," 
                + "'"+ nv.MaLoaiNhanVien.ToString() +"') ";

            // Thuc thi cau lenh insert
            if (writer.ThucThiDuLieu(insertSQL))
                return true;
            else
                return false;
        }

        // Ham sua thong tin cua 1 nhan vien theo MaNV
        public static Boolean SuaTTNhanVien(DTO.CNHANVIEN nv,int MaNV)
        {
            DAO.CDataProvider writer = new DAO.CDataProvider();

            // Cau lenh SQL
            String updateSQL = "";
            updateSQL+=" UPDATE NHANVIEN "
                + " SET HoTen = '" + nv.HoTen.ToString() + "', "
                + " Tuoi = " + nv.Tuoi + ","
                + " NgaySinh = '" + nv.NgaySinh.ToString() + "',"
                + " DiaChi = '" + nv.DiaChi.ToString() + "',"
                + " MaLoaiNV = '" + nv.MaLoaiNhanVien.ToString() + "' "
                + " WHERE MaNhanVien =" + MaNV + " " ;
            String a = updateSQL;
            // Thuc thi cau lenh insert
            if (writer.ThucThiDuLieu(updateSQL))
                return true;
            else
                return false;
        }

        // Ham xoa 1 nhan vien theo MaNV
        public static Boolean XoaNhanVien(int MaNV)
        {
            DAO.CDataProvider writer = new DAO.CDataProvider();

            // Cau lenh SQL
            String deleteSQL = " DELETE FROM NHANVIEN "
                + " WHERE MaNhanVien =" + MaNV + " ";

            // Thuc thi cau lenh insert
            if (writer.ThucThiDuLieu(deleteSQL))
                return true;
            else
                return false;
        }

    }

}
